// mainPackage/pages/evaluate/evaluate.js
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: 0,
        numText: '零',
        text: '',
        productId: 0,
    },

    // 提交
    submit() {

        var fileList = this.data.fileList,
            that = this;

        if (this.data.text == '') {
            wx.showToast({
                title: '请输入评价',
                icon: 'none'
            })
            return
        }
        if (this.data.value == 0) {
            wx.showToast({
                title: '请选择星级',
                icon: 'none'
            })
            return
        }


        console.log(this.data.value);
        console.log(this.data.text);


        var obj = {
            commentContent: this.data.text,
            userId: app.globalData.userId,
            productId: this.data.productId,
            star: this.data.value,
        }

        console.log(obj);
        return
        that.addTalk(obj)


    },

    addTalk(obj) {
        console.log(obj);

        var url = app.globalData.url + "ScComment/addComment",
            that = this;
        app.wxRequestJson(url, obj, res => {
            console.log(res);
            if (res.flag) {
                that.chgangeProStatus()
                wx.showModal({
                    title: '提示',
                    content: '评价成功',
                    showCancel: false,
                    complete: (res) => {
                        if (res.confirm) {
                            wx.redirectTo({
                                url: '/packageProduct/pages/orderList/orderList?index=' + 3,
                            })
                        }
                    }
                })
            } else {
                wx.showToast({
                    title: '发布失败,请重试',
                    icon: 'none'
                })
            }

        },
            err => {
                console.log(err);
            })
    },

    onChange(e) {
        console.log(e);
        this.setData({
            value: e.detail,
            numText: this.numberToChinese(e.detail)
        })
    },



    // 输入框编辑
    inputHandle(e) {
        var str = e.currentTarget.dataset.flag
        var val = e.detail.value
        this.setData({
            [str]: val
        })
        console.log(this.data);
    },

    numberToChinese(num) {
        const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
        const chineseUnits = ['', '十', '百', '千'];

        if (num === 0) {
            return chineseNums[0];
        }

        let chineseStr = '';
        let unitIndex = 0;

        while (num > 0) {
            const digit = num % 10;
            if (digit !== 0) {
                // 处理非零数字
                chineseStr = chineseNums[digit] + chineseUnits[unitIndex] + chineseStr;
            } else if (chineseStr.charAt(0) !== chineseNums[0]) {
                // 处理连续的零，只保留一个零
                chineseStr = chineseNums[0] + chineseStr;
            }

            num = Math.floor(num / 10);
            unitIndex++;
        }

        return chineseStr;
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        this.setData({
            productId: Number(options.id),
        })
        console.log(this.data.productId);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})